package us.esme.managers{
	import com.asfusion.mate.events.Dispatcher;
	
	import us.esme.events.ESMEEvent;
	import us.esme.events.StatusEvent;
	
	
	public class SessionManager{
			
		public static const LOGGED_IN:String = "logged_in"; 
		public static const LOGGED_OUT:String = "logged_out"; 
		
		/* stores the current session state */
		[Bindable] public var sessionState:String;
		[Bindable] public var currentUser_fullname:String;
		[Bindable] public var currentUser_nickname:String;
		[Bindable] public var currentUser_image:String;
		
		
		public function SessionManager(){
			sessionState = LOGGED_OUT;
		}
		
		
		public function setloggedIn():void{
			sessionState = LOGGED_IN;
		}
		
		public function setloggedOut():void{
			sessionState = LOGGED_OUT;
		}
		
		public function setState(obj:Object):void{
			
			var se:StatusEvent = new StatusEvent(StatusEvent.SET_STATUS);
			
			if(obj.esme_api != null){
				
				if(obj.esme_api.success){ // if call was successful
					
					if(obj.esme_api.operation == "login"){
						setloggedIn();
						
						
						se.status = "Login Successful .. ";
						new Dispatcher().dispatchEvent(se);
						
						/* dispatch get messages */
						var getMessagesEvent:ESMEEvent = new ESMEEvent(ESMEEvent.GET_MESSAGES);
						new Dispatcher().dispatchEvent(getMessagesEvent);
						
						var getTrackingListEvent:ESMEEvent = new ESMEEvent(ESMEEvent.GET_TRACKING);
						new Dispatcher().dispatchEvent(getTrackingListEvent);
						
						
						
					}
					
					if(obj.esme_api.operation == "logout"){
						setloggedOut();
					}
					
				}else{
					se.status = "Failed !";
					new Dispatcher().dispatchEvent(se);
					
				}
				
			}else {
				
					se.status = "Failed !";
					new Dispatcher().dispatchEvent(se);
			}
		}
		
		
		
		public function setCurrentUser(obj:Object):void{
			
			if(obj.esme_api != null){
				if(obj.esme_api.success){ // if call was successful
				      currentUser_image = obj.esme_api.user.image;
				      currentUser_nickname = obj.esme_api.user.nickname;
				      currentUser_fullname = obj.esme_api.user.whole_name;

				}
			}
			
		}
		
		

	}
}